﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECP.Model;
using ECP.Service.Utilities;
using ECP.Utilities.Controls;
using ECP.Client.Permission;
using ECP.Utilities;

namespace ECP.Client.Module
{
    public partial class ModulePermissionAdminFrm : FormBase, IModelInfo
    {
        private string moduleFullName;
        private DataTable DTPermission;
        private bool PermissionAccess;
        private bool PermissionAdd;
        private bool PermissionDelete;
        private bool PermissionEdit;
        public ModulePermissionAdminFrm()
        {
            this.DTPermission = new DataTable(BasePermissionTable.TableName);
            this.InitializeComponent();
        }

        public ModulePermissionAdminFrm(string moduleID)
            : this()
        {
            this.ucModuleSelect.SelectModule.selectedID = moduleID;
            BaseModuleEntity entityParent = new BaseModuleEntity();
           
            var DTParent = ServiceManager.Instance.ModuleService.Get(base.UserInfo, moduleID);
        
            var selectModule = new UCModuleSelect.SelectModuleInfo();
            entityParent.GetFrom(DTParent);
          
            selectModule.selectedID = entityParent.ID;
            selectModule.selectedFullName = entityParent.FullName;
       
        }

        public ModulePermissionAdminFrm(string moduleID, string moduleFullName)
            : this()
        {
            this.ucModuleSelect.SelectModule.selectedID = moduleID;
            this.ucModuleSelect.SelectModule.selectedFullName = moduleFullName;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            this.Remove();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            PermissionSelectFrm form = new PermissionSelectFrm();
            form.AllowNull = false;
            form.MultiSelect = true;
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ServiceManager.Instance.ModuleService.BatchAddPermissions(base.UserInfo, this.TargetModuleID, form.SelectedIDs);
                this.FormOnLoad();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataRowView view in this.DTPermission.DefaultView)
            {
                view.Row[BaseBusinessLogic.SelectedColumn] = true.ToString();
            }
        }


        private void ModulePermissionAdminFrm_Load(object sender, EventArgs e)
        {
            this.FormOnLoad();
        }


        private void FormOnLoad()
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.DTPermission = ServiceManager.Instance.ModuleService.GetPermissionDT(base.UserInfo, this.TargetModuleID);
                base.Localization(this);
                this.GetPermission();
                this.BindData();
                this.SetControlState();
            }
            catch (Exception exception)
            {
                base.ProcessException(exception);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void BindData()
        {
            if (this.TargetModuleID.Length > 0)
            {
                this.ucModuleSelect.SelectModule.selectedID = this.TargetModuleID;
                this.ucModuleSelect.SelectModule.selectedFullName = this.TargetModuleFullName;
            }
            BaseInterfaceLogic.DataTableAddColumn(this.DTPermission, BaseBusinessLogic.SelectedColumn);
          
            this.grdPermission.DataSource = this.DTPermission.DefaultView;
        }
        private void GetPermission()
        {
            this.PermissionAccess = base.IsAuthorized("ModuleAdmin");
            this.PermissionAdd = base.IsAuthorized("ModuleAdmin");
            this.PermissionEdit = base.IsAuthorized("ModuleAdmin");
            this.PermissionDelete = base.IsAuthorized("ModuleAdmin");
        }

        private string[] GetSelecteIDs()
        {
            return BaseInterfaceLogic.GetSelecteIDs(this.DTPermission, BasePermissionTable.FieldID, BaseBusinessLogic.SelectedColumn);
        }
        public int Remove()
        {
            int num = 0;
            if (BaseInterfaceLogic.CheckInputSelectAnyOne(this.DTPermission, BaseBusinessLogic.SelectedColumn) && (MessageBox.Show(AppMessage.MSG0075, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK))
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    num = ServiceManager.Instance.ModuleService.BatchDletePermissions(base.UserInfo, this.TargetModuleID, this.GetSelecteIDs());
                    this.DTPermission = ServiceManager.Instance.ModuleService.GetPermissionDT(base.UserInfo, this.TargetModuleID);
                    this.BindData();
                    this.SetControlState();
                }
                catch (Exception exception)
                {
                    base.ProcessException(exception);
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
            return num;
        }

        private void SetControlState()
        {
            this.ucModuleSelect.AllowNull = false;
            this.ucModuleSelect.SimpleAdmin = true;
            this.btnRemove.Enabled = false;
            this.btnSelectAll.Enabled = false;
            this.btnInvertSelect.Enabled = false;
            this.btnAdd.Enabled = this.PermissionAdd;
            if (this.DTPermission.DefaultView.Count > 0)
            {
                this.btnSelectAll.Enabled = this.PermissionEdit;
                this.btnInvertSelect.Enabled = this.PermissionEdit;
                this.btnRemove.Enabled = this.PermissionDelete;
            }
        }

        private string TargetModuleFullName
        {
            get
            {
                return this.ucModuleSelect.SelectModule.selectedFullName;
            }
            set
            {
                this.ucModuleSelect.SelectModule.selectedFullName = value;
            }
        }

        private string TargetModuleID
        {
            get
            {
                return this.ucModuleSelect.SelectModule.selectedID;
            }
            set
            {
                this.ucModuleSelect.SelectModule.selectedID = value;
            }
        }


        public IList<PermissionInfo> Permissions
        {
            get
            {
                return new List<PermissionInfo>()
                {
                    new PermissionInfo("ModuleAdmin", "权限模块关联") ,
                  
                };
            }
        }
        public override string ModelCode
        {
            get { return this.GetType().FullName; }
        }

        public override string ModelName
        {
            get { return this.Text; }
        }

        public override void StartModel()
        {
            base.ShowDialog();
        }

        private void btnInvertSelect_Click(object sender, EventArgs e)
        {
            foreach (DataRowView view in this.DTPermission.DefaultView)
            {
                bool flag = true;
                if (view.Row[BaseBusinessLogic.SelectedColumn].ToString().Equals(flag.ToString()))
                {
                    view.Row[BaseBusinessLogic.SelectedColumn] = false.ToString();
                }
                else
                {
                    view.Row[BaseBusinessLogic.SelectedColumn] = true.ToString();
                }
            }
        }

    }
}